package com.gee.lowui;

import javax.microedition.lcdui.*;

public class PullList extends GControl
{
	private Button but;

	private Listbox list;

	private Image[] img;

	public boolean isPullList = false;

	public PullList(Image[] image, String[] str)
	{
		this.img = image;
		Image temp = Image.createImage(img[0].getWidth(), img[0].getHeight());
		Graphics gt = temp.getGraphics();
		gt.setColor(0x5B0101);
		gt.fillRect(0, 0, temp.getWidth(), temp.getHeight());
		gt.setColor(0x8A6E6E);
		gt.drawRect(0, 0, temp.getWidth() - 1, temp.getHeight() - 1);
		list = new Listbox(new Image[] { temp }, null, str);
		list.isDrawScrollBar = false;
		list.isFillBack = true;
		list.setAnchor(Graphics.TOP | Graphics.LEFT);
		if (str.length > 0)
		{
			this.controlString = str[list.getCurrentIndex()];
		}
		but = new Button(img, controlString);
		but.setAnchor(Graphics.TOP | Graphics.LEFT);
		but.initializePostion();
	}

	public void setListColor(int[] color)
	{
		list.setListColor(color);
	}

	public void setIcon(Image[] icon, int[] index)
	{
		list.setIcon(icon, index);
	}

	public int getSelectIndex()
	{
		return list.getCurrentIndex();
	}

	public void setString(String[] str)
	{
		list.setListString(str);
		but.setString(list.controlString);
		this.controlString = but.getCurrentString();
	}

	public int getControlType()
	{
		return GControl.CONTROL_PULLLIST;
	}

	public void setIsPullList(boolean is)
	{
		isPullList = is;
	}

	protected void setAction(boolean is)
	{
		isAction = is;
		but.setAction(is);
		list.setAction(is);
	}

	public void moveIndex(int step)
	{
		list.moveIndex(step);
		but.setString(list.controlString);
		this.controlString = but.getCurrentString();
		this.index = list.index;
	}

	public void setPostion(int ex, int ey)
	{
		x = ex;
		y = ey;
		but.setPostion(x, y);
		list.setPostion(x, y + img[0].getHeight() + 3);
		initializePostion();
	}

	public void drawControl(Graphics g)
	{
		if (isAction)
		{
			if (isPullList)
			{
				but.isAction = false;
				list.isAction = true;
			}
			else
			{
				but.isAction = true;
			}
		}
		if (list.isColorString)
		{
			but.fontColor = list.colorSet[list.index];
		}
		else
		{
			but.fontColor = 0xC2B797;
		}
		but.drawControl(g);
		if (list.indexIcon != null && list.imgIcon != null)
		{
			if (list.imgIcon[list.indexIcon[index]] != null)
			{
				g.drawImage(list.imgIcon[list.indexIcon[index]], controlX + 10, //
						controlY + (g.getFont().getHeight() - list.imgIcon[list.indexIcon[index]].getHeight()) / 2, Graphics.TOP | Graphics.LEFT);
			}
		}
		if (isPullList)
		{
			list.drawControl(g);
		}
	}
}